const app = getApp()

Page({
  data: {
    currDate: '2018-08-05',
    startDate: '',
    endDate: '',
    navbar: ['今日提交', '历史数据'],
    currentTab: 0,
    nickName: "",
    avatarUrl: "",
    bloodSugarArray: ['早餐前', '早餐后', '午餐前', '午餐后', '晚餐前', '晚餐后', '睡觉前'],
    moodArray: ['沮丧', '兴奋', '开心', '生气'],
    bloodSugarArrayIndex: 0,
    moodArrayIndex: 0,
    /*参数 */
    bloodHigh: '',
    bloodLow: '',
    bloodSugarNum: '',
    bloodSugarType: '早餐前',
    weight: '',
    urine: '',
    dailyMood: '沮丧',
    medicineBack: '',
    code: 0,
    isHiddenContent: false,
    goBack: '<返回',
    btnContent: '提交',
    diaryId: 0,
    diaryItems: [{
      id: '1',
      uid: '',
      time: '2018-05-03:23-23-23',
      bp_high: '135',
      bp_low: '83',
      gls_type: '123',
      gls: '456',
      upd: '123',
      weight: '86',
      mood: '不错',
      medicine: '还好',
    }],
  },

  onLoad: function() {
    var date = this.getNowFormatDate();
    this.setData({
      currDate: date,
      startDate: date,
      endDate: date
    })
    wx.setNavigationBarTitle({
      title: app.globalData.Titles[1] //页面标题为路由参数
    })
    this.getWeight();
  },
  navbarTap: function(e) {
    var that = this;
    that.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    if (that.data.currentTab == 1) {
      //获取历史数据
      this.getAllHistoryData();
    }
  },
  tapName: function(event) {
    if (event.currentTarget.id == "blood_high") {

    }
  },
  //血糖
  bindBloodSugarPickerChange: function(e) {
    this.setData({
      bloodSugarArrayIndex: e.detail.value
    })

  },
  //今日心情
  bindMoodPickerChange: function(e) {
    this.setData({
      moodArrayIndex: e.detail.value
    })
  },
  //提交今日日记
  todaySubmit(e) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/diary/save',
      data: {
        uid: app.globalData.USERID,
        bp_high: e.detail.value.bloodHigh == '' ? null : e.detail.value.bloodHigh,
        bp_low: e.detail.value.bloodLow == '' ? null : e.detail.value.bloodLow,
        gls: e.detail.value.bloodSugarNum == '' ? null : e.detail.value.bloodSugarNum,
        gls_type: that.data.bloodSugarArray[that.data.bloodSugarArrayIndex],
        weight: e.detail.value.weight == '' ? null : e.detail.value.weight,
        upd: e.detail.value.urine == '' ? null : e.detail.value.urine,
        mood: that.data.moodArray[that.data.moodArrayIndex],
        medicine: e.detail.value.medicineBack == '' ? null : e.detail.value.medicineBack,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.result) {
          wx.showToast({
            title: '提交成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
          /*wx.navigateTo({
            url: '../index/index',
          })*/
        }
        //console.log(res);
      },
      fail: function(res) {
        //console.log(res)
      }
    })
  },
  //开始时间
  bindDateChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      startDate: e.detail.value
    })
  },
  //结束时间
  bindDateChange2: function(e) {
    console.log(e.detail.value)
    this.setData({
      endDate: e.detail.value
    })
  },
  //获得当前时间
  getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  },
  //查询上一次用户填写的体重
  getWeight: function() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/diary',
      data: {
        uid: app.globalData.USERID,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.weight != '0') {
          that.setData({
            weight: res.data.weight,
          })
        }
        //console.log(res);
      },
      fail: function(res) {
        //console.log(res)
      }
    })
  },
  //查询功能
  dataQuery() {
    //
    wx.showToast({
      title: '查找中',
      icon: 'loading',
      duration: 10000,
      mask: true
    })
    //
    var that = this;
    wx.request({
      url: app.globalData.URL + '/diary/searchDiary',
      data: {
        uid: app.globalData.USERID,
        startDate: that.data.startDate,
        endDate: that.data.endDate,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        wx.hideToast();
        //console.log(res);
        that.setData({
          diaryItems: res.data.diarys,
        })
      },
      fail: function(res) {
        //console.log(res)
      }
    })
  },
  //查看日记详情
  goToDetail: function(event) {

    var that = this;
    var index = event.currentTarget.dataset.index;
    var item = that.data.diaryItems[index];
    for (var i = 0; i < that.data.bloodSugarArray.length; i++) {
      if (that.data.bloodSugarArray[i] == item.gls_type) {
        that.setData({
          bloodSugarArrayIndex: i,
        })
        break;
      }
    }
    for (var i = 0; i < that.data.moodArray.length; i++) {
      if (that.data.moodArray[i] == item.mood) {
        that.setData({
          moodArrayIndex: i,
        })
        break;
      }
    }
    console.log(that.data.bloodSugarArrayIndex)
    //console.log(that.data.bloodSugarArrayIndex + "," + that.data.moodArrayIndex)
    that.setData({
      diaryId: item.id,
      isHiddenContent: true,
      bloodHigh: item.bp_high,
      bloodLow: item.bp_low,
      bloodSugarNum: item.gls,
      bloodSugarType: item.gls_type,
      weight: item.weight,
      urine: item.upd,
      dailyMood: item.mood,
      medicineBack: item.medicine,
    })
    //console.log(event)
  },
  goBackToData: function() {
    var that = this;
    that.setData({
      isHiddenContent: false,
    })
  },
  //获取所有的历史数据
  getAllHistoryData: function() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/diary/myDiary',
      data: {
        uid: app.globalData.USERID,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        //console.log(res);
        if (res.data.result) {
          that.setData({
            diaryItems: res.data.diarys,
          })
        }
      },
      fail: function(res) {
        //console.log(res)
      }
    })
  },
  //修改疾病日记
  modiTodaySubmit: function(e) {
    var that = this;
    //console.log(e)
    wx.request({
      url: app.globalData.URL + '/diary/updateDiary',
      data: {
        id: that.data.diaryId,
        bp_high: e.detail.value.bloodHigh == '' ? null : e.detail.value.bloodHigh,
        bp_low: e.detail.value.bloodLow == '' ? null : e.detail.value.bloodLow,
        gls: e.detail.value.bloodSugarNum == '' ? null : e.detail.value.bloodSugarNum,
        gls_type: that.data.bloodSugarArray[that.data.bloodSugarArrayIndex],
        weight: e.detail.value.weight == '' ? null : e.detail.value.weight,
        upd: e.detail.value.urine == '' ? null : e.detail.value.urine,
        mood: that.data.moodArray[that.data.moodArrayIndex],
        medicine: e.detail.value.medicineBack == '' ? null : e.detail.value.medicineBack,

      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.result) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1000,
            mask: true
          })
          that.getAllHistoryData();
          
        }

      },
      fail: function(res) {
        //console.log(res)
      }
    })
  },
  delDiary: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/diary/delete',
      data: {
        id: that.data.diaryId,
        },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.result) {
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000,
            mask: true
          })
          that.getAllHistoryData();
          that.goBackToData();
        }
      },
      fail: function(res) {
        //console.log(res)
      }
    })
  }
})